<?php

// TODO: Generalizar esto a un abmcontrller
class Ext_Controller_LovController extends Ext_Controller_SecureAction
{
    /**
     * Model class to use for data access
     * @var string
     */
    protected $_lovModel = "Ext_Model_Lov";

    /**
     * If false the id is autgenerated other wise the user can chose what ever he wants.
     * @var boolean
     */
    protected $_editID = true;

    /**
     * Title to use in the edit view.
     * @var string
     */
    protected $_editTitle = "Aqui podra editar los datos.";

    /**
     * Title to use in the list view.
     * @var string
     */
    protected $_listTitle = "El siguiente listado muestra los valores elija uno para editarlo.";

    /**
     * Title to use in the list view on create link.
     * @var string
     */
    protected $_listEditTitle = "Crear un nuevo dato";

    /**
     * Cols to show in the list view with attributes.
     * @var array
     */
    protected $_col = array(    "id"            => "ID",
                                "description"   => "Valor",
    );
    protected $_listColTitleAttr = array(   "id"            => 'width="30%" align="left"',
                                            "description"   => 'width="50%" align="left"',
    );
    protected $_listColAttr = array(    "id"                => 'valign="top"',
                                        "description"       => 'valign="top"',
    );

    /**
     * Actions to show in the list view with attributes.
     * @var array
     */
    protected $_listActions = true;
    protected $_listActionTitle = "Accion";
    protected $_listActionTitleAttr = 'width="20%" align="left"';
    protected $_listAction = array( "delete" => "Borrar" );
    protected $_listActionAttr = array( "delete" => 'valign="top"' );

    public function listAction()
    {
        $lovDb = Ext_Generic_Model::get( $this->_lovModel, $this->_db );
        $paginator = new Ext_Paginator( new Zend_Paginator_Adapter_Iterator( $lovDb->fetchAll( null, 'description' ) ) );
        $paginator->setCurrentPageNumber( $this->_request->getParam( "page" ) );
        $viewScript = $this->_helper->viewRenderer->getViewScript();


        if ( $this->view->getScriptPath( $viewScript ) === false ) {
            $this->_helper->viewRenderer->setViewScriptPathSpec( "lov/list.phtml" );
        }

        $this->view->paginator = $paginator;
        $this->view->title = $this->_listTitle;

        $this->view->cols = $this->_col;
        $this->view->colsTitleAttr = $this->_listColTitleAttr;
        $this->view->colsAttr = $this->_listTitleAttr;


        if ( $this->_listActions ) {
            $this->view->actions = $this->_listAction;
            $this->view->actionsTitle = $this->_listActionTitle;
            $this->view->actionsTitleAttr = $this->_listActionTitleAttr;
            $this->view->actionsAttr = $this->_listActionAttr;
        }
        
        $this->view->listEditTitle = $this->_listEditTitle;
    }


    public function editAction()
    {
        $viewScript = $this->_helper->viewRenderer->getViewScript();
        if ( $this->view->getScriptPath( $viewScript ) === false ) {
            $this->_helper->viewRenderer->setViewScriptPathSpec("lov/edit.phtml");
        }

        $lovDb = Ext_Generic_Model::get( $this->_lovModel, $this->_db );
        $form = new Ext_Forms_BasicForm( );

        $lovId = $this->_request->getParam( 'lovId' );
        if ( is_null( $lovId ) ) {
            $this->view->lov = $lovDb->createRow();
            if ( $this->_editID ) {
                $element = new Zend_Form_Element_Text( 'new_id' );
                $element->setLabel( $this->_col[ "id" ] )->setRequired( true )->addValidator( 'NotEmpty' );
            } else {
                $element = new Zend_Form_Element_Hidden( 'new_id' );
            }
            if ( $lovDb instanceof Ext_Model_IdLov ) {
                $element->addValidator( 'Int' );
            }
            $element->setValue( $lovDb->getNextId() );
            $element->addValidator( $lovDb->getValidator() );
            $form->addElement( $element );

        } else {
            $this->view->lov = $lovDb->find( $lovId )->current();
            if ( empty( $this->view->lov ) ) {
                $this->addError( "Lov not found" );
                $this->_redirect( $this->view->surl( null, 'list' ) );
                return;
            }
        }

        $element = new Zend_Form_Element_Text( 'description' );
        $element->setLabel( $this->_col[ 'description' ] )->setRequired( true )->addValidator( 'NotEmpty' );
        $form->addElement( $element );

        $form->cancel->setLabel( 'Cancelar' )->setAttrib( 'onclick', "document.location='" .
            $this->view->surl( null, 'list' ) . "'");
        $form->accept->setLabel( 'Aceptar' );

        $data = $this->_request->getParams();

        if ( $this->_request->isPost() ) {
            if ( $form->isValid( $data ) ) {
                $extraData = array( "description" => $data[ "description" ] );
                if ( isset( $data[ "new_id" ] ) ) {
                    $extraData[ "id" ] = $data["new_id"];
                }
                $this->view->lov->setFromArray( $extraData );
                $this->view->lov->save();
                $this->_redirect( $this->view->surl( null, 'list' ) );
            }
        }

        $data[ 'description' ] = $this->view->lov->description;
        $form->setAction( $this->view->surl( null, 'edit', array( 'lovId' => $lovId ) ) );
        $form->populate( $data );
        $this->view->form = $form;
        
        $this->view->title = $this->_editTitle;
    }



    private function validateId()
    {
        $lovId = $this->_request->getParam( 'lovId' );
        if ( is_null( $lovId ) ) {
            $this->addError( "Id not found" );
            return false;
        }

        $lovDb = Ext_Generic_Model::get( $this->_lovModel, $this->_db );
        $this->view->lov = $lovDb->find( $lovId )->current();
        if ( empty( $this->view->lov ) ) {
            $this->addError( "Id not found" );
            return false;
        }
        return true;
    }

    public function deleteAction()
    {
        if ( $this->validateId() ) {
            $this->view->lov->delete();
        }
        $this->_redirect( $this->view->surl( null, 'list') );
        return;
    }
}